<?php

namespace app\result;

use app\model\user\AmountType;

class AmountTypeResult extends BaseResult
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new  AmountType();
    }

    private function init($user_id)
    {
        if (cache('amount_type_init_' . $user_id)) {
            return;
        }
        cache('amount_type_init_' . $user_id, 1, 10);
        if (!$this->model->get_value(['user_id' => $user_id], 'id')) {
            $data = [
                ['name' => '微信', 'type' => 10, 'user_id' => $user_id],
                ['name' => '支付宝', 'type' => 10, 'user_id' => $user_id],
                ['name' => '银行卡', 'type' => 10, 'user_id' => $user_id],
                ['name' => '现金', 'type' => 10, 'user_id' => $user_id],
                ['name' => '信用卡', 'type' => 10, 'user_id' => $user_id],
                ['name' => '其他', 'type' => 10, 'user_id' => $user_id],
                ['name' => '微信', 'type' => 20, 'user_id' => $user_id],
                ['name' => '支付宝', 'type' => 20, 'user_id' => $user_id],
                ['name' => '银行卡', 'type' => 20, 'user_id' => $user_id],
                ['name' => '其他', 'type' => 20, 'user_id' => $user_id],
            ];
            $this->model->data_add($data, true);
        }
    }

    public function list($user_id, $args = [])
    {
        $keyword = $args['keyword'] ?? '';
        $where = [['user_id', '=', $user_id]];
        if ($keyword) {
            $where[] = ['name', 'like', "%$keyword%"];
        }
        $this->init($user_id);
        $data = $this->model->data_list_nopage($where, 'id,name,sort,type', 'sort desc,id desc');

        return self::success_return('success', [
            'list' => $data,
        ]);
    }

    public function get($user_id, $args = [])
    {
        $id = $args['id'] ?? 0;
        if (!$id) {
            return self::error_return('数据错误');
        }
        $data = $this->model->data_get(['id' => $id, 'user_id' => $user_id]);
        return self::success_return('success', [
            'info' => $data,
        ]);
    }

    public function save($user_id, $args)
    {
        $id = $args['id'] ?? 0;
        $name = $args['name'] ?? '';
        if (!$name) {
            return self::error_return('请填写名称');
        } elseif (mb_strlen($name, 'utf-8') > 10) {
            return self::error_return('名称不能大于10个字');
        }
        $baseData = [
            'name' => $name,
            'sort' => intval($args['sort'] ?? 0),
            'type' => intval($args['type'] ?? 10),
            'user_id' => $user_id,
        ];
        if ($id) {
            $res = $this->model->data_update(['id' => $id], $baseData);
        } else {
            $res = $this->model->data_add($baseData);
        }
        if (!$res) {
            return self::error_return('保存失败');
        }
        return self::success_return('保存成功');
    }

    public function delete($user_id, $args)
    {
        $id = $args['id'] ?? 0;
        if (!$id) {
            return self::error_return('请选择要删除的数据');
        }
        $data = $this->model->data_get(['id' => $id]);
        if (!$data || $data['user_id'] != $user_id) {
            return self::error_return('只能删除自己的数据');
        }
        $this->model->data_del(['id' => $id]);
        return self::success_return('删除成功');
    }
}
